package com.niuxiaofa.newlaw.Activity;


import android.content.Context;
import android.content.SharedPreferences;
import android.widget.EditText;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.niuxiaofa.newlaw.Base.BaseActivity;
import com.niuxiaofa.newlaw.Bean.UserLoginBean;
import com.niuxiaofa.newlaw.Network.UserPresenter;
import com.niuxiaofa.newlaw.R;
import com.niuxiaofa.newlaw.Tools.Constant;
import com.niuxiaofa.newlaw.Tools.GetLog;
import com.niuxiaofa.newlaw.Tools.SystemUtil;
import com.niuxiaofa.newlaw.Tools.ToActivityUtil;
import com.niuxiaofa.newlaw.adapter.AutoPollAdapter;
import com.niuxiaofa.newlaw.adapter.AutoPollRecyclerView;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import cn.pedant.SweetAlert.SweetAlertDialog;
import www.linwg.org.lib.LCardView;


public class UserNameLoginActivity extends BaseActivity {

    @BindView(R.id.recyclerview)
    AutoPollRecyclerView recyclerView;


    @BindView(R.id.phone)
    EditText phone;

    @BindView(R.id.password)
    EditText password;


    @BindView(R.id.login)
    LCardView login;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_login_username;
    }


    SharedPreferences sharedPreferences;


    @Override
    protected void initData() {

        setSystemInvadeBlack(false);
        sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);

        recyclerView.setLayoutManager(
                new LinearLayoutManager(
                        this,
                        //设置LinearLayoutManager.HORIZONTAL  则水平滚动
                        LinearLayoutManager.VERTICAL, false));
        AutoPollAdapter autoPollAdapter = new AutoPollAdapter(getApplicationContext());
        recyclerView.setAdapter(autoPollAdapter);
        //启动滚动
        recyclerView.start();


        //  phone = getIntent().getStringExtra("phone");

        login.setOnClickListener(e -> {

            Map deviceMap = new HashMap();
            deviceMap.put("osType", "android");
            deviceMap.put("os", SystemUtil.getSystemVersion());
            deviceMap.put("serialNumber", "");
            deviceMap.put("pushToken", "123456");

            Map map = new HashMap();
            map.put("userName", phone.getText().toString());
            map.put("type", "");
            map.put("accessToken", "");
            map.put("unionid", "");
            map.put("nickName", "");
            map.put("password", password.getText().toString());
            map.put("deviceLogin", deviceMap);


            UserPresenter.login(map, new UserPresenter.interface_login() {
                @Override
                public void getResponse(UserLoginBean u) {

                    if (u.getCode() == 1) {
                        Constant.TOKEN = u.getData().getToken();
                        Toast(u.getMsg());
                        GetLog.getLog(Constant.TOKEN);
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putString("token", Constant.TOKEN);
                        editor.commit();
                        ToActivityUtil.toNextActivity(MainActivity.class);
                    } else {
                        new SweetAlertDialog(getActivity(), SweetAlertDialog.CUSTOM_IMAGE_TYPE)
                                .setTitleText("")
                                .setContentText(u.getMsg())
                               .show();
                    }
                }
            });
        });


    }


}
